class Solution {    
    int res = 0;
public:
    void dfs(TreeNode* root, int depth) {
        if (root->left == nullptr && root->right == nullptr) {
            res = max(res, depth);
            return;
        }
        if (root->left) dfs(root->left, depth + 1);
        if (root->right) dfs(root->right, depth + 1);
    }
    int maxDepth(TreeNode* root) {
        if (root == nullptr) return 0;
        dfs(root, 1);
        return res;
    } 
};